package com.noble.glamour.ui.agora.act.video;

import android.content.Context;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.faceunity.agora.VideoPreProcessing;
import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.light.UserInfoLightweight;
import com.noble.glamour.bean.my.ChatInfo;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.custom.ScaleTextView;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.ui.agora.RtcUIShow;
import com.noble.glamour.ui.agora.act.bean.ChatStatus;
import com.noble.glamour.ui.agora.act.bean.RtcGiftList;
import com.noble.glamour.ui.agora.model.EngineConfig;
import com.noble.glamour.ui.agora.util.RtcJointChannel;
import com.noble.glamour.ui.agora.util.RtcTimerUtil;
import com.noble.glamour.ui.agora.view.RtcGiftInfoView;
import com.noble.library.image.ImageLoader;
import com.noble.library.view.base.BasePanel;
import com.noble.library.view.roadlights.LMarqueeFactory;
import com.noble.library.view.roadlights.LMarqueeView;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频聊天头部浮层布局
 * Created by Su on 2017/5/31.
 */
public class RtcVideoHeadPanel extends BasePanel implements View.OnClickListener, RequestComplete {
    private boolean openCamera = false;     // 首次摄像头开关状态
    private ImageView ivCamera, iv_head, iv_complain, iv_addBuddy;    // 切换摄像头
    private TextView tv_age, tv_name;
    private RelativeLayout rl_girlEarn;
    private ScaleTextView tv_earn;

    private ChatInfo chatInfo;
    private EngineConfig config;

    private LMarqueeView lMarqueeView;
    private LMarqueeFactory<LinearLayout, RtcGiftList.GiftInfo> marqueeView;

    public RtcVideoHeadPanel(Context context, boolean openCamera, UserInfoLightweight infoLight) {
        super(context);
        setContentView(R.layout.f1_chat_video_header_panel);
        this.openCamera = openCamera;
        initView();
        reqUserInfo();
    }

    private void reqUserInfo() {
        List<Long> uids = new ArrayList<>();
        uids.add(config.mOtherId);
        ModuleMgr.getCommonMgr().reqUserInfoSummary(uids, this);
    }

    private void initView() {
        enableFaceU();  // 开启faceunity
        config = ModuleMgr.getRtcEnginMgr().getEngineConfig();
        tv_age = (TextView) findViewById(R.id.tv_age);
        tv_name = (TextView) findViewById(R.id.tv_name);
        iv_head = (ImageView) findViewById(R.id.iv_head);
        iv_complain = (ImageView) findViewById(R.id.iv_chat_complain);
        iv_addBuddy = (ImageView) findViewById(R.id.iv_add_buddy);

        // 礼物跑马灯
        lMarqueeView = (LMarqueeView) findViewById(R.id.rtc_chat_marquee);
        marqueeView = new RtcGiftInfoView(getContext());

        // 女性用户
        if (!ModuleMgr.getCenterMgr().getMyInfo().isMan()) {
            iv_complain.setVisibility(View.GONE);  // 无投诉功能
            iv_addBuddy.setVisibility(View.GONE);  // 女性不展示加好友

            // 赚钱收益
            rl_girlEarn = (RelativeLayout) findViewById(R.id.rl_girl_earn);
            tv_earn = (ScaleTextView) findViewById(R.id.tv_earn);
            rl_girlEarn.setVisibility(View.VISIBLE);
            tv_earn.setText(getContext().getString(R.string.chat_video_girl_earn, config.mGirlEarn));
        }

        ivCamera = (ImageView) findViewById(R.id.btn_switch_camera);
        Chronometer chatCountUp = (Chronometer) findViewById(R.id.ch_count_time);
        chatCountUp.setBase(SystemClock.elapsedRealtime() - config.millisPass);
        RtcTimerUtil.getInstance().addTimer(chatCountUp);
        chatCountUp.start(); // 开启通信计时

        iv_head.setOnClickListener(this);
        ivCamera.setOnClickListener(this);
        iv_complain.setOnClickListener(this);
        iv_addBuddy.setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);
        findViewById(R.id.iv_shrink_narrow).setOnClickListener(this);
        if (!openCamera && ModuleMgr.getCenterMgr().getMyInfo().isMan())
            ivCamera.setVisibility(View.GONE);

        RtcJointChannel.getInstance().reqChatInfo(this);
        ModuleMgr.getRtcEnginMgr().reqGiftBanner(this);  // 获取跑马灯礼物

        iv_head = (ImageView) findViewById(R.id.iv_head);
    }

    private void refreshView() {
        ImageLoader.loadCircleAvatar(getContext(), chatInfo.getOtherInfo().getAvatar(), iv_head);
        tv_name.setText(chatInfo.getOtherInfo().getNickname());
        tv_age.setText(getContext().getString(R.string.user_info_age, chatInfo.getOtherInfo().getAge()));
        // 如果是好友，隐藏
        if (chatInfo.getOtherInfo().isGiftFriend()) {
            iv_addBuddy.setVisibility(View.GONE);
        }

        ModuleMgr.getRtcEnginMgr().getEngineConfig().nickname = chatInfo.getOtherInfo().getNickname();
    }

    public void refreshCamera(boolean isClose) {
        if (isClose) {
            ivCamera.setVisibility(View.GONE);
            return;
        }
        ivCamera.setVisibility(View.VISIBLE);
    }

    /**
     * 女性用户刷新收益
     */
    public void refreshEarn(ChatStatus chatStatus) {
        if (chatStatus.getTotalIncome() <= 0) return;
        tv_earn.setScaleText(getContext().getString(R.string.chat_video_girl_earn, config.mGirlEarn));
    }

    /**
     * 添加好友
     */
    public void refreshAddBuddy() {
        iv_addBuddy.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_head:
                break;

            case R.id.btn_close:
                RtcJointChannel.getInstance().stopChat();
                break;

            case R.id.btn_switch_camera:
                RtcJointChannel.getInstance().switchCamera();
                break;

            case R.id.iv_shrink_narrow:
                if (listener != null)
                    listener.onPanelClick(v);
                break;

            case R.id.iv_chat_complain:  // 投诉
                RtcUIShow.showRtcVideoCommentDlg((FragmentActivity) getActivity());
                break;

            case R.id.iv_add_buddy:      // 添加好友
                RtcUIShow.showRtcVideoGiftDlg((FragmentActivity) getActivity());
                break;
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqChatInfo) {
            if (!response.isOk()) return;
            chatInfo = (ChatInfo) response.getBaseData();
            refreshView();
            return;
        }

        if (response.getUrlParam() == UrlParam.reqGiftBanner) {
            if (!response.isOk()) return;
            RtcGiftList list = (RtcGiftList) response.getBaseData();
            List<RtcGiftList.GiftInfo> giftList = list.getGiftList();
            if (giftList != null && !giftList.isEmpty()) {
                lMarqueeView.setVisibility(View.VISIBLE);
                marqueeView.setData(ModuleMgr.getRtcEnginMgr().removeInvalidGift(giftList));
                lMarqueeView.setAnimInAndOut(R.anim.top_in, R.anim.bottom_out);
                lMarqueeView.setMarqueeFactory(marqueeView);
                lMarqueeView.startFlipping();
            }
        }
    }

    /**
     * 是否启用faceunity
     */
    private static VideoPreProcessing mVideoPreProcessing;

    private void enableFaceU() {
        if (mVideoPreProcessing == null) {
            mVideoPreProcessing = new VideoPreProcessing();
            mVideoPreProcessing.enablePreProcessing(true);
        }
    }

    // ===================== 回调监听 ==========================
    private PanelListener listener;

    public void setPanelListener(PanelListener listener) {
        this.listener = listener;
    }

    public interface PanelListener {
        void onPanelClick(View v);
    }
}
